package com.zr.subclass.controller;

import com.zr.subclass.model.pojo.SubClass;
import com.zr.subclass.model.pojo.SubClassUpdateVo;
import com.zr.subclass.model.pojo.SubPlanClassAddVo;
import com.zr.subclass.service.SubClassService;
import com.zr.utils.page.PageParameterUtil;
import com.zr.utils.page.PageUtil;
import com.zr.utils.result.ResultBuilderUtil;
import com.zr.utils.result.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @ClassName PlanClassController
 * @Description 用于计划大类的请求和响应
 * @Author 马家奎
 * @Date 2019/11/7 18:53
 */
@CrossOrigin
@RestController
@RequestMapping("subClass")
public class SubClassController {
    @Autowired
    private SubClassService subClassService;
    /*
    * @Author 马家奎
    * @Description 多条件+分页查询
    * @Param  PlanClassVo
    * @return ResultVo
    **/
    @PostMapping("queryPage")
    public ResultUtil queryPage(@RequestBody PageParameterUtil pageParameterUtil){

        PageUtil<SubClass> page =  subClassService.queryPage(pageParameterUtil);
        return ResultBuilderUtil.success(page);

    }

    @GetMapping("queryById")
    public ResultUtil queryById(Integer id){


        SubClass planClass  =  subClassService.queryById(id);

        return ResultBuilderUtil.success(planClass);

    }
    /*
    * @Author 马家奎
    * @Description //TODO 添加计划大类信息 * @Date
    * @Param status code name
    * @return ResultVo
    **/
    @PostMapping("add")
    public ResultUtil addPlanClass(@RequestBody @Valid SubPlanClassAddVo subPlanClassAddVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderUtil.error(bindingResult.getFieldError().getDefaultMessage());
        }else{

            return subClassService.addPlanClass(subPlanClassAddVo);
        }
    }
    /*
    * @Author 马家奎
    * @Description //TODO 修改计划大类信息 * @Date
    * @Param status name
    * @return
    **/
    @PostMapping("update")
    public ResultUtil updatePlanClass(@RequestBody @Valid SubClassUpdateVo subClassUpdateVo, BindingResult bindingResult ){

        return subClassService.updatePlanClass(subClassUpdateVo);
    }
    /*
    * @Author 马家奎
    * @Description 操作启用禁用
    * @Param
    * @return
    **/
    @PostMapping("updateStatus")
    public ResultUtil planClassStatus(@RequestBody SubClass planClass){

        return   subClassService.planClassStatus(planClass);
    }

}
